package com.example.springbootbook.service;

import com.example.springbootbook.enums.BookStatusEnum;
import com.example.springbootbook.mapper.BookMapper;
import com.example.springbootbook.model.BookInfo;
import com.example.springbootbook.model.PageRequest;
import com.example.springbootbook.model.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class BookService {
    @Autowired
    private BookMapper bookMapper;

    public PageResult<BookInfo> selectBookInfoByPage(PageRequest pageRequest) {
        if (pageRequest == null) return null;
        Integer total = bookMapper.getTotal();
        List<BookInfo> bookInfos = bookMapper.getBookInfoByPage(pageRequest.getOffset(),pageRequest.getPageSize());
        if (bookInfos != null && !bookInfos.isEmpty()) {
            for (BookInfo bookInfo : bookInfos) {
                bookInfo.setStatusCN(BookStatusEnum.getNameByCode(bookInfo.getStatus()).getName());
            }
        }
        return new PageResult<>(bookInfos,total,pageRequest);
    }

    public Integer addBook(BookInfo bookInfo) {
        return bookMapper.addBook(bookInfo);
    }

    public BookInfo queryBookInfoById(Integer id) {
        return bookMapper.queryBookInfoById(id);
    }

    public Integer updateBook(BookInfo bookInfo) {
        return bookMapper.updateBook(bookInfo);
    }
}
